package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.domain.CldSubject;
import com.cleaningcloud.cld.mapper.CldSubjectMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.common.security.utils.SubjectConversionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldAccountDetailsMapper;
import com.cleaningcloud.cld.domain.CldAccountDetails;
import com.cleaningcloud.cld.service.ICldAccountDetailsService;

/**
 * 账户流水明细Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-04-21
 */
@Service
public class CldAccountDetailsServiceImpl implements ICldAccountDetailsService
{
    @Autowired
    private CldAccountDetailsMapper cldAccountDetailsMapper;
    @Autowired
    private CldSubjectMapper cldSubjectMapper;

    /**
     * 查询账户流水明细
     * 
     * @param accountDetailsId 账户流水明细主键
     * @return 账户流水明细
     */
    @Override
    public CldAccountDetails selectCldAccountDetailsByAccountDetailsId(Long accountDetailsId)
    {
        CldAccountDetails obj = cldAccountDetailsMapper.selectCldAccountDetailsByAccountDetailsId(accountDetailsId);
        if(null != obj){
            if(obj.getOperaType() == 1){
                obj.setSubjectId1(obj.getSubjectId());
            }else{
                obj.setSubjectId2(obj.getSubjectId());
            }
        }
        return obj;
    }

    /**
     * 查询账户流水明细列表
     * 
     * @param cldAccountDetails 账户流水明细
     * @return 账户流水明细
     */
    @Override
    public List<CldAccountDetails> selectCldAccountDetailsList(CldAccountDetails cldAccountDetails)
    {
        List<CldAccountDetails> list = cldAccountDetailsMapper.selectCldAccountDetailsList(cldAccountDetails);
        if(!list.isEmpty()){
            for(CldAccountDetails accountDetails : list){
                if(null != accountDetails.getSubjectId()){
                    accountDetails.setSubjectName(SubjectConversionUtils.conversion(accountDetails.getSubjectId()));
                    Long subjectId = Long.valueOf(accountDetails.getSubjectId().substring(accountDetails.getSubjectId().lastIndexOf(",") + 1));
                    CldSubject subject = cldSubjectMapper.selectCldSubjectBySubjectId(subjectId);
                    if(null != subject){
                        accountDetails.setSubjectNo(subject.getSubjectNo());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增账户流水明细
     * 
     * @param cldAccountDetails 账户流水明细
     * @return 结果
     */
    @Override
    public int insertCldAccountDetails(CldAccountDetails cldAccountDetails)
    {
        if(cldAccountDetails.getOperaType() == 1){
            cldAccountDetails.setSubjectId(cldAccountDetails.getSubjectId1());
        }else{
            cldAccountDetails.setSubjectId(cldAccountDetails.getSubjectId2());
        }
        cldAccountDetails.setCreateTime(DateUtils.getNowDate());
        cldAccountDetails.setCreateId(SecurityUtils.getUserId());
        cldAccountDetails.setCreateName(SecurityUtils.getUsername());
        cldAccountDetails.setFlag(0L);
        return cldAccountDetailsMapper.insertCldAccountDetails(cldAccountDetails);
    }

    /**
     * 修改账户流水明细
     * 
     * @param cldAccountDetails 账户流水明细
     * @return 结果
     */
    @Override
    public int updateCldAccountDetails(CldAccountDetails cldAccountDetails)
    {
        if(cldAccountDetails.getOperaType() == 1){
            cldAccountDetails.setSubjectId(cldAccountDetails.getSubjectId1());
        }else{
            cldAccountDetails.setSubjectId(cldAccountDetails.getSubjectId2());
        }
        return cldAccountDetailsMapper.updateCldAccountDetails(cldAccountDetails);
    }

    /**
     * 批量删除账户流水明细
     * 
     * @param accountDetailsIds 需要删除的账户流水明细主键
     * @return 结果
     */
    @Override
    public int deleteCldAccountDetailsByAccountDetailsIds(Long[] accountDetailsIds)
    {
        return cldAccountDetailsMapper.deleteCldAccountDetailsByAccountDetailsIds(accountDetailsIds);
    }

    /**
     * 删除银行流水明细信息
     * 
     * @param accountDetailsId 银行流水明细主键
     * @return 结果
     */
    @Override
    public int deleteCldAccountDetailsByAccountDetailsId(Long accountDetailsId)
    {
        return cldAccountDetailsMapper.deleteCldAccountDetailsByAccountDetailsId(accountDetailsId);
    }
}
